package com.miao.admin.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Message;
import com.miao.admin.service.MessageService;
import com.miao.common.utils.JWTUtil;
import com.miao.websecurity.entity.Admin;
import com.miao.websecurity.service.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 公告记录表(Message)表控制层
 *
 * @author makejava
 * @since 2021-02-12 22:14:02
 */
@RestController
@RequestMapping("admin/message")
public class MessageController {
    /**
     * 服务对象
     */
    @Resource
    private MessageService messageService;


    /**
     * 发布消息
     * @param message 消息
     * @return R
     */
    @PostMapping("/release")
    public R release(@RequestBody  Message message ,@RequestHeader(value = "access-token") String token){

        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        message.setSendTime(new Date());
        message.setAdminId(Integer.valueOf(map.get("adminId").toString()));

        if (this.messageService.save(message)) {
            return R.ok();
        }else {
            return R.error();
        }


    }


    /**
     * 获取全部消息
     * @return list
     */
    @GetMapping("/all")
    public R all(){
        List<Message> list = this.messageService.list();
        return R.ok(list);
    }

}
